package com.hf.javaBasics;

import java.io.*;

public class TestIOStream {

    /*
     * 文件-FileOutputStream节点流-Buffer处理流（缓冲）- Data处理流 基本类型的读写 - Object对象流 （对象读写） flush 写入文件
     * 读取，
     * 反过来套接
     * */
    public static void testStream() throws IOException {
        // 节点流FileOutputStream直接以A.txt作为数据源操作
        FileOutputStream fileOutputStream = new FileOutputStream("A.txt");
        // 过滤流BufferedOutputStream进一步装饰节点流，提供缓冲写
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        // 过滤流DataOutputStream进一步装饰过滤流，使其提供基本数据类型的写
        DataOutputStream out = new DataOutputStream(bufferedOutputStream);
        out.writeInt(3);
        out.writeBoolean(true);
        // Flushes this data output stream. This forces any buffered output bytes to be written out to the stream
        out.flush();
        out.close();
        // 此处输入节点流，过滤流正好跟上边输出对应，读者可举一反三
        DataInputStream in = new DataInputStream(new BufferedInputStream(
                new FileInputStream("A.txt")));
        System.out.println(in.readInt());
        System.out.println(in.readBoolean());
        in.close();
    }
    public static void fileTest() throws IOException {
        // 如果存在，就直接使用，不存在就创建一个新的
        File file = new File("D:a.txt");
        if (!file.exists()) {
            boolean newFile = file.createNewFile();
            System.out.println(newFile);
        }
        System.out.println(file.getName());

        // 创建文件夹
        file.mkdir();
        // 创建多级文件夹
        file.mkdirs();
    }

    public static void ReadFileInputStreamOneByOne() {
        //创建一个File对象，拿到我们的数据
        File file = new File("F:/a.txt");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("文件已存在");
            try {
                //read的返回值为-1说明文件读取完毕
                FileInputStream fileInputStream = new FileInputStream(file);
                //read的读取方式为一个个的读取，也就是说你调用一次read就会读取下一个字节
                int i = fileInputStream.read();
                while (i > 0){
                    System.out.println((char)i);
                    i = fileInputStream.read();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void ReadFileInputStreamByKb() {
        //以byte数组方式读取
        File file = new File("F:/a.txt");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("文件已存在");
            byte[] bytes = new byte[1024];  //1kb

            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                //以一个kb的方式去读取，如果文本内容不超过一个kb的话就会全部读取，将读取到的内容放在byte数组中并且返回文本内容的字节数
                int read = fileInputStream.read(bytes);
                String s = new String(bytes);

                System.out.println(s);
                System.out.println(read);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 这段代码一定要记住了，烂熟于心
     * FileInputStream - InputStreamReader - BufferedReader
     *  BufferedReader.readLine()
     * @throws IOException
     */
    public static void testBufferReader() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }

    }

    /**
     * FileOutputStream - OutputStreamWriter - BufferedWriter
     */
    public static void testBufferWriter() {
        File file = new File("F:/a.txt");
        File file1 = new File("F:/b.txt");
        if (!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("文件已存在");
            try{
                //使用InputStreamReader
                FileInputStream fileInputStream = new FileInputStream(file);
                //获取字符输入流
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                // 字节流的写入
                FileOutputStream fileOutputStream = new FileOutputStream(file1);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
                String line = null;
                while ((line=bufferedReader.readLine())!= null){
                    System.out.println(line);
                    bufferedWriter.write(line);
                    bufferedWriter.newLine();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
